package com.tky.conference.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.tky.conference.R;
import com.tky.conference.base.BaseApplication;
import com.tky.conference.dao.AppointInfo;
import com.tky.conference.dao.DeptInfo;
import com.tky.conference.dao.HysInfo;
import com.tky.conference.dao.TreeInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.entity.LoginOutInfo;
import com.tky.conference.implInterfaces.DeleteImpl;
import com.tky.conference.implInterfaces.DeptInfoImpl;
import com.tky.conference.implInterfaces.HysInfoImpl;
import com.tky.conference.implInterfaces.TreeInfoImpl;
import com.tky.conference.interfaces.HttpResultListener;
import com.tky.conference.ui.AddActivity;
import com.tky.conference.ui.ConferenceActivity;
import com.tky.conference.ui.UpdateActivity;
import com.tky.conference.utlils.ShowDialog;
import com.tky.conference.utlils.ToastUtil;
import com.tky.conference.utlils.UIUtils;

import java.io.Serializable;
import java.util.List;

/**
 * Created by yy on 2015/12/7.
 */
public class ConferenceAdapter extends BaseAdapter {
    private Activity context;
    private List<AppointInfo> appointInfoList;
    private boolean b1,b2,b3;
    private Intent intent;
    private ShowDialog showDialog;
    private List<TreeInfo> TreeInfoList;
    private List<HysInfo> hysInfoList;
    private List<DeptInfo> deptInfoList;
    private String account;
    private DeleteImpl deleteImpl;
    private LoginOutInfo loginOutInfobj;
    private int currentModifyItemPosition = -1;

    public ConferenceAdapter(Activity context,List<AppointInfo> appointInfoList,String account){
        this.context=context;
        this.appointInfoList=appointInfoList;
        this.account=account;
        showDialog=new ShowDialog(context);
    }

    @Override
    public int getCount() {
        return appointInfoList.size();
    }

    @Override
    public Object getItem(int i) {
        return getItem(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        final AppointInfo appointInfo=appointInfoList.get(position);
        if (view==null){
            view=View.inflate(context, R.layout.conference_item,null);
            holder=new ViewHolder();
            holder.project= (TextView) view.findViewById(R.id.project);
            holder.destination= (TextView) view.findViewById(R.id.destination);
            holder.appoint= (TextView) view.findViewById(R.id.appoint);
            holder.time= (TextView) view.findViewById(R.id.time);
            holder.update= (Button) view.findViewById(R.id.conference_update);
            holder.delete= (Button) view.findViewById(R.id.conference_delete);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }

        holder.project.setText(appointInfo.getHyzt());
        holder.destination.setText(appointInfo.getHydd());
        if (appointInfo.getStatus().equals(1)){
            holder.appoint.setText("预约");
        }else if (appointInfo.getStatus().equals(0)){
            holder.appoint.setText("已取消");
        }
        holder.time.setText(appointInfo.getStime());

        //修改的点击事件

        holder.update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                currentModifyItemPosition = position;
                intent=new Intent(context, UpdateActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("APPINFO", appointInfo);
                intent.putExtras(bundle);
                //1.先请求数的数据
                b1=false;
                b2=false;
                b3=false;
                DeptInfoImpl deptInfoImpl = new DeptInfoImpl(new DeptHttpResultListener());
                deptInfoImpl.getTreeConstruct(account);

                HysInfoImpl hysInfoImpl = new HysInfoImpl(new HysHttpResultListener());
                hysInfoImpl.hysInfo(account);

                TreeInfoImpl treeInfoImpl = new TreeInfoImpl(new TreeHttpResultListener());
                treeInfoImpl.getTreeInfo(account);
            }
        });

        //删除请求网络
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(context);
                builder.setTitle("删除")
                        .setMessage("确认要删除吗")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                deleteImpl = new DeleteImpl(new HttpResultListener() {
                                    @Override
                                    public void onStartRequest() {

                                    }

                                    @Override
                                    public void onResult(Object obj) {
                                        if (obj instanceof FailRequest) {
                                            if (!((FailRequest) obj).getStatus().equals(0)) {
                                                Toast.makeText(context, ((FailRequest) obj).getMsg(), Toast.LENGTH_SHORT).show();
                                            }
                                        }else {
                                            loginOutInfobj = (LoginOutInfo) obj;
                                            if(loginOutInfobj.getStatus().equals("0")){
                                                Log.i("删除事件响应。。。", "okokok");
                                                appointInfoList.remove(appointInfo);
                                                notifyDataSetChanged();
                                            }
                                        }
                                    }

                                    @Override
                                    public void onFinish() {

                                    }
                                });
                                Log.i("当前删除item的id",appointInfo.getId());
                                deleteImpl.delete(appointInfo.getId());
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        }).show();
            }
        });


        return view;
    }


    class TreeHttpResultListener implements HttpResultListener {


        @Override
        public void onStartRequest() {
            showDialog.Loading("加载数据...");
        }

        @Override
        public void onResult(Object obj) {
            if (obj instanceof FailRequest) {
                showDialog.dismissDialog();
                if (!((FailRequest) obj).getStatus().equals(0)) {
                    Toast.makeText(context,((FailRequest) obj).getMsg(),Toast.LENGTH_LONG).show();
                }
            } else {

                TreeInfoList = (List<TreeInfo>) obj;
                intent.putExtra("TREELIST", (Serializable) TreeInfoList);
                b1=true;
                canGo();
            }


        }

        @Override
        public void onFinish() {

        }
    }

    class DeptHttpResultListener implements HttpResultListener{

        @Override
        public void onStartRequest() {
            showDialog.Loading("加载数据...");
        }

        @Override
        public void onResult(Object obj) {
            if (obj instanceof FailRequest) {
                showDialog.dismissDialog();
                if (!((FailRequest) obj).getStatus().equals(0)) {
                }
            } else {
                deptInfoList = (List<DeptInfo>) obj;
                intent.putExtra("DEPTLIST", (Serializable) deptInfoList);
                b2=true;
                canGo();
            }

        }

        @Override
        public void onFinish() {

        }
    }

    class HysHttpResultListener implements HttpResultListener {

        @Override
        public void onStartRequest() {
            showDialog.Loading("加载数据...");
        }

        @Override
        public void onResult(Object obj) {
            if (obj instanceof FailRequest) {
                showDialog.dismissDialog();
                if (!((FailRequest) obj).getStatus().equals(0)) {

                }
            } else {
                hysInfoList=(List<HysInfo>)obj;
                intent.putExtra("HYSLIST", (Serializable) hysInfoList);
                b3=true;
                canGo();

            }
        }

        @Override
        public void onFinish() {

        }
    }

    public void canGo(){
        if (b1&b2&b3){
            showDialog.dismissDialog();
            context.startActivityForResult(intent, 1001);
        }
    }



    class ViewHolder{
        TextView project;
        TextView time;
        TextView appoint;
        TextView destination;
        Button update;
        Button delete;
    }

    public int getCurrentModifyItemPosition(){
        return currentModifyItemPosition;
    }
}
